package com.bilibili.juc.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 使用了completableFuture.complete("completeValue")方法来尝试设置 CompletableFuture 的结果值为 "completeValue"。
 这个方法会立即将 CompletableFuture 的结果设置为指定的值，不管原先的异步操作是否已经完成。
 */
public class CompletableFutureAPI1Demo
{
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
            //传入一个供给型函数 ，没参数有返回值Supplier<U>
            CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
                //暂停几秒钟线程
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "abc";
            });

//            System.out.println(completableFuture.get());
//            System.out.println(completableFuture.get(2L,TimeUnit.SECONDS));
            //System.out.println(completableFuture.join());

            //暂停几秒钟线程
            //try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }

            //System.out.println(completableFuture.getNow("xxx"));
            System.out.println(completableFuture.complete("completeValue")+"\t"+completableFuture.get());

    }


}
